package cn.seaboot.admin.file.web;

import cn.seaboot.admin.file.bean.AppInfo;
import cn.seaboot.admin.upload.Uploader;
import cn.seaboot.admin.file.service.AppInfoService;
import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.upload.OutputInfo;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * 系统APP文件 [t_sys_app_info] - Controller
 * <p>
 *
 * @author ChenSS on 2018-06-20.
 * @version 1.0.0
 */
@Controller
@RequestMapping("sys/app")
@Tag(name = "系统APP文件 [t_sys_app_info] 管理")
public class AppInfoCtrl {

    @Resource
    private AppInfoService appInfoService;

    private final static String STORAGE = "app";

    /**
     * 增
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统APP文件 [t_sys_app_info] 增")
    @RequestMapping(value = "info", method = RequestMethod.POST)
    public int saveInfo(@Validated AppInfo pojo,
                        @RequestParam(required = false) MultipartFile file) throws IOException {
        OutputInfo info = Uploader.storage(STORAGE).write(file);
        pojo.setAppPath(info.getPath());
        return appInfoService.insert(pojo);
    }

    /**
     * 删
     *
     * @param id id
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统APP文件 [t_sys_app_info] 删")
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(@Params String id) {
        return appInfoService.deleteById(id);
    }

    /**
     * 查
     *
     * @param id id
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "系统APP文件 [t_sys_app_info] 查")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public AppInfo queryById(@Params String id) {
        return appInfoService.queryById(id);
    }

    /**
     * 改
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统APP文件 [t_sys_app_info] 改")
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int updateInfo(@Validated AppInfo pojo, @Params String id,
                          @RequestParam(required = false) MultipartFile file) throws IOException {
        AppInfo old = appInfoService.queryById(pojo.getId());
        if (file != null) {
            String path = Uploader.storage(STORAGE).overwrite(file, old.getAppPath());
            pojo.setAppPath(path);
        }
        return appInfoService.updateById(pojo);
    }

    /**
     * 查列表
     *
     * @param params 参数
     * @return PageInfo
     */
    @ResponseBody
    @Operation(description = "系统APP文件 [t_sys_app_info] 列表查询")
    @Parameters({
            @Parameter(name = "page", description = "页号"),
            @Parameter(name = "limit", description = "行数")})
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<AppInfo> queryPage(@RequestParam Map<String, Object> params) {
        return Pagination.apply(params, map-> appInfoService.queryList(map));
    }
}
